package com.boss.salary.controller;

import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.common.entity.PageDTO;
import com.boss.common.util.PageUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.framework.exception.AppRuntimeException;
import com.boss.salary.service.IReportFormService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 报表控制器
 */
@RestController
@RequestMapping(value = "/reportFormController", produces = MediaType.APPLICATION_JSON)
public class ReportFormController extends BasicinfoBaseController {

    private static final Logger LOG = LoggerFactory.getLogger(ReportFormController.class);

    @Autowired
    private IReportFormService reportFormService;

    /**
     * 工资签报
     * @return
     */
    @GetMapping("/salarySign")
    public ApiResultDTO salarySign(){
        try {
            List<Map<String, Object>> result = reportFormService.salarySign();
            return ApiResultDTO.success(result);
        }catch (AppRuntimeException e){
            LOG.error(e.getMessage(),e);
            return ApiResultDTO.error(ERROR_RSCODE, e.getMessage());
        }catch(Exception e){
            LOG.error(e.getMessage(),e);
            return ApiResultDTO.error(ERROR_RSCODE, "获取工资签报失败");
        }
    }

    @GetMapping("/queryUnitSalarySummaryPage")
    public ApiResultDTO queryUnitSalarySummaryPage(@RequestParam(value = "page") String page,
                                                   @RequestParam(value = "limit") String limit,
                                                   @RequestParam("salaryMonth") String salaryMonth,
                                                   @RequestParam("salaCardBank") String salaCardBank){
        try{
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page, limit);
            PageDTO<Map<String, Object>> pageDTO = reportFormService.getUnitSalarySummaryPage(paginationDTO, salaryMonth, salaCardBank);
            return ApiResultDTO.success(pageDTO);
        }catch(Exception e){
            LOG.error(e.getMessage(),e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询失败");
        }
    }


    /**
     * 按单位汇总工行、交行离休
     * @param page
     * @param limit
     * @param salaryMonth
     * @param bankCode
     * @return
     */
    @GetMapping("/queryUnitBankRetiredPage")
    public ApiResultDTO queryUnitBankRetiredPage(@RequestParam(value = "page") String page,
                                                 @RequestParam(value = "limit") String limit,
                                                 @RequestParam(value = "salaryMonth", required = false) String salaryMonth,
                                                 @RequestParam(value = "bankCode", required = false) String bankCode) {
        try{
            Map<String, Object> queryParam = new HashMap<>(2);
            queryParam.put("salaryMonth", salaryMonth);
            queryParam.put("bankCode", bankCode);
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page, limit);
            PageDTO<Map<String, Object>> pageDTO = reportFormService.queryUnitBankRetiredPage(paginationDTO, queryParam);
            return ApiResultDTO.success(pageDTO);
        }catch(Exception e){
            LOG.error(e.getMessage(),e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询失败");
        }
    }

    /**
     * 按单位汇总工行、交行在职
     * @param page
     * @param limit
     * @param salaryMonth
     * @param bankCode
     * @return
     */
    @GetMapping("/queryUnitBankWorkPage")
    public ApiResultDTO queryUnitBankWorkPage(@RequestParam(value = "page") String page,
                                                 @RequestParam(value = "limit") String limit,
                                                 @RequestParam(value = "salaryMonth", required = false) String salaryMonth,
                                                 @RequestParam(value = "bankCode", required = false) String bankCode) {
        try{
            Map<String, Object> queryParam = new HashMap<>(2);
            queryParam.put("salaryMonth", salaryMonth);
            queryParam.put("bankCode", bankCode);
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page, limit);
            PageDTO<Map<String, Object>> pageDTO = reportFormService.queryUnitBankWorkPage(paginationDTO, queryParam);
            return ApiResultDTO.success(pageDTO);
        }catch(Exception e){
            LOG.error(e.getMessage(),e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询失败");
        }
    }

    /**
     * 按单位汇总交通补贴
     * @param page
     * @param limit
     * @param salaryMonth
     * @param bankCode
     * @return
     */
    @GetMapping("/vwSalAgencyPersonTrafficAllowance")
    public ApiResultDTO vwSalAgencyPersonTrafficAllowance(@RequestParam(value = "page") String page,
                                              @RequestParam(value = "limit") String limit,
                                              @RequestParam(value = "salaryMonth", required = false) String salaryMonth,
                                              @RequestParam(value = "bankCode", required = false) String bankCode) {
        try{
            Map<String, Object> queryParam = new HashMap<>(2);
            queryParam.put("salaryMonth", salaryMonth);
            queryParam.put("bankCode", bankCode);
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page, limit);
            PageDTO<Map<String, Object>> pageDTO = reportFormService.vwSalAgencyPersonTrafficAllowance(paginationDTO, queryParam);
            return ApiResultDTO.success(pageDTO);
        }catch(Exception e){
            LOG.error(e.getMessage(),e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询失败");
        }
    }

    /**
     * 人员变更新增
     * @param page
     * @param limit
     * @param salaryMonth
     * @return
     */
    @GetMapping("/vwSalPersonChangeAdd")
    public ApiResultDTO vwSalPersonChangeAdd(@RequestParam(value = "page") String page,
                                                   @RequestParam(value = "limit") String limit,
                                                   @RequestParam("salaryMonth") String salaryMonth){
        try{
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page, limit);
            PageDTO<Map<String, Object>> pageDTO = reportFormService.vwSalPersonChangeAdd(paginationDTO, salaryMonth);
            return ApiResultDTO.success(pageDTO);
        }catch(Exception e){
            LOG.error(e.getMessage(),e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询失败");
        }
    }

    /**
     * 人员变更删除
     * @param page
     * @param limit
     * @param salaryMonth
     * @return
     */
    @GetMapping("/vwSalPersonChangeDelete")
    public ApiResultDTO vwSalPersonChangeDelete(@RequestParam(value = "page") String page,
                                                   @RequestParam(value = "limit") String limit,
                                                   @RequestParam("salaryMonth") String salaryMonth){
        try{
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page, limit);
            PageDTO<Map<String, Object>> pageDTO = reportFormService.vwSalPersonChangeDelete(paginationDTO, salaryMonth);
            return ApiResultDTO.success(pageDTO);
        }catch(Exception e){
            LOG.error(e.getMessage(),e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询失败");
        }
    }

    /**
     * 人员变动调转
     * @param page
     * @param limit
     * @param salaryMonth
     * @return
     */
    @GetMapping("/queryPersonChangeTurnPage")
    public ApiResultDTO queryPersonChangeTurnPage(@RequestParam(value = "page") String page,
                                                  @RequestParam(value = "limit") String limit,
                                                  @RequestParam(value = "salaryMonth", required = false) String salaryMonth){
        try {
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page, limit);
            Map<String, Object> queryParam = new HashMap<>(2);
            queryParam.put("salaryMonth", salaryMonth);
            PageDTO<Map<String, Object>> pageDTO = reportFormService.queryPersonChangeTurnPage(paginationDTO, queryParam);
            return ApiResultDTO.success(pageDTO);
        } catch (AppRuntimeException e) {
            LOG.error(e.getMessage(),e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询失败");
        }
    }

}

